/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.ExpertManualService;
import com.chlitina.store.modules.expert.service.ExpertManualServiceService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 专家人工分析Controller
 * 
 * @author lvbo
 * @version 2013-07-09
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/expert/expertManualService")
public class ExpertManualServiceController extends BaseController {

	@Autowired
	private ExpertManualServiceService expertManualServiceService;

	@ModelAttribute
	public ExpertManualService get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return expertManualServiceService.get(id);
		} else {
			return new ExpertManualService();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(ExpertManualService expertManualService,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Store store = StoreUtils.getStore(true);
		if (store != null) {
			expertManualService.setStore(store);
		}
		String status = request.getParameter("status");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		Page<ExpertManualService> page = expertManualServiceService.find(
				new Page<ExpertManualService>(request, response),
				expertManualService, status, startDate, endDate);
		int expertManualAmount = expertManualServiceService.getExpertManualAmount();
		model.addAttribute("expertManualAmount", store.getExpertManualAmount() - expertManualAmount);
		model.addAttribute("status", status);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("page", page);
		return "modules/expert/expertManualServiceList";
	}

	@RequestMapping(value = "form")
	public String form(ExpertManualService expertManualService, Model model) {
		model.addAttribute("expertManualService", expertManualService);
		return "modules/expert/expertManualServiceForm";
	}
	
	@RequestMapping(value = "detail")
	public String detail(ExpertManualService expertManualService, Model model) {
		if (expertManualService.getAnswerDatetime() != null) {
			expertManualService.setAnswerDatetime(new java.sql.Date(expertManualService.getAnswerDatetime().getTime()));
		}
		if (expertManualService.getAskDatetime() != null) {
			expertManualService.setAskDatetime(new java.sql.Date(expertManualService.getAskDatetime().getTime()));
		}
		model.addAttribute("expertManualService", expertManualService);
		return "modules/expert/expertManualServiceDetail";
	}
	@RequestMapping(value = "save")
	public String save(ExpertManualService expertManualService, Model model,
			RedirectAttributes redirectAttributes) {
		expertManualService.setAskDatetime(new Timestamp(new Date().getTime()));
		if (!beanValidator(model, expertManualService)) {
			return form(expertManualService, model);
		}
		expertManualServiceService.save(expertManualService);
		addMessage(redirectAttributes, "保存请求成功");
		return "redirect:" + Global.ADMIN_PATH
				+ "/expert/expertManualService/";
	}

	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		expertManualServiceService.delete(id);
		addMessage(redirectAttributes, "删除请求成功");
		return "redirect:" + Global.ADMIN_PATH
				+ "/expert/expertManualService/";
	}

}
